package com.fate.securecheckplatform.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.fate.securecheckplatform.SecureCheckApplication;

import java.util.ArrayList;
import java.util.List;

public class SpUtil {

    private static String PREFERENCE_NAME = "SCP_HELPER";
    private static SharedPreferences sharedPreferences;
    private static SpUtil instance = null;

    public static SpUtil getInstance() {
        if (instance == null) {
            instance = new SpUtil();
            sharedPreferences = SecureCheckApplication.get().getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        }
        return instance;
    }


    public boolean putString(String key, String value) {
        return sharedPreferences.edit().putString(key, value).commit();
    }

    /**
     * 获取保存的String型数据
     *
     * @param key 保存数据时的key
     * @return String型数据
     */
    public String getString(String key) {
        return sharedPreferences.getString(key, null);
    }


    /**
     * 保存int型数据
     *
     * @param key   key值
     * @param value 数据
     * @return 布尔型
     */
    public boolean putInt(String key, int value) {
        return sharedPreferences.edit().putInt(key, value).commit();
    }

    /**
     * 获取int型数据
     *
     * @param key key
     * @return int
     */
    public int getInt(String key) {
        return sharedPreferences.getInt(key, -1);
    }


    /**
     * 保存long型数据
     *
     * @param key   key值
     * @param value 保存的数据
     * @return 布尔型
     */
    public boolean putLong(String key, long value) {
        return sharedPreferences.edit().putLong(key, value).commit();
    }

    /**
     * 获取long型数据
     *
     * @param key key值
     * @return long
     */
    public long getLong(String key) {
        return sharedPreferences.getLong(key, -1);
    }

    /**
     * 保存float类型数据
     *
     * @param key   key
     * @param value value
     * @return 布尔型
     */
    public boolean putFloat(String key, float value) {
        return sharedPreferences.edit().putFloat(key, value).commit();
    }

    /**
     * 获取float型数据
     *
     * @param key key
     * @return float
     */
    public float getFloat(String key) {
        return sharedPreferences.getFloat(key, -1);
    }

    /**
     * 保存boolean数据
     *
     * @param key   key
     * @param value value
     * @return boolean
     */
    public boolean putBoolean(String key, boolean value) {
        return sharedPreferences.edit().putBoolean(key, value).commit();
    }

    /**
     * 获取boolean值
     *
     * @param key key
     * @return boolean
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    /**
     * 保存List
     *
     * @param key      key
     * @param dataList list
     */
    public boolean putList(String key, List<String> dataList) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("int", dataList.size());
        for (int i = 0; i < dataList.size(); i++) {
            editor.putString(key + i, dataList.get(i));
        }
        return editor.commit();

    }

    /**
     * 获取List
     *
     * @param key key
     * @return list
     */
    public List<String> getList(String key) {
        List<String> environmentList = new ArrayList<>();
        int environNums = sharedPreferences.getInt("int", 0);
        for (int i = 0; i < environNums; i++) {
            String environItem = sharedPreferences.getString(key + i, null);
            environmentList.add(environItem);
        }
        return environmentList;
    }

}
